package com.evil.common.application.enums.robot;

import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

/**
 * 机器人触发动作
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum RobotTriggerTypeEnum {

    /**
     * 机器人触发动作
     */
    ADD(1, "新增数据", RobotRuleTypeEnum.ADD),
    MODIFY(2, "更新数据", RobotRuleTypeEnum.MODIFY),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;
    @Getter
    private final RobotRuleTypeEnum robotRuleTypeEnum;

    public static Optional<RobotTriggerTypeEnum> findById(Integer id) {
        if (Objects.isNull(id)) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static RobotTriggerTypeEnum isExistById(Integer id) {
        return RobotTriggerTypeEnum.findById(id).orElseThrow(RobotTriggerTypeEnum::getBusinessException);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "机器人触发动作参数错误");
    }
}
